import Vue from 'vue'
import Vuex from 'vuex'
import { login } from '@/api'
import router from '../router'
import { Message } from 'element-ui'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {

  },
  mutations: {

  },
  actions: {
    async login({ commit }, payload) {
      const username = payload.username
      const password = payload.password
      const res = await login({ username, password });
      console.log(res);
      if (res.data.code === 0) {
        localStorage.setItem("token", res.data.token);
        router.push("/");
        Message({
          message: res.data.msg,
          type: 'success'
        }, true)
      } else {
        Message({
          message: res.data.msg,
          type: 'error'
        }, true)
      }
    }
  },
  modules: {
  }
})
